<?php
header('Content-Type: application/json');
Header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Origin:*');   //跨域

require '../mysql_init.php';

//判断年龄
function limit_age($age){
    if (is_numeric($age) && $age >= 0 && $age < 300) {
        return true;
    } else {
        return false;
    }
}
//判断ID
function limit_ID($ID){
    $flag=false;
    $pattern = '/^[1-9]\d{16}[\dXx]/';
    $birth_year=substr($ID,6,4);
    $birth_month=substr($ID,10,2);
    $birth_day=substr($ID,12,2);
    $new_year=date("Y");
    if(preg_match($pattern, $ID)==1){
        if($new_year-$birth_year>0 && $birth_month>=1 && $birth_month<=12 && $birth_day>=1 && $birth_day<=31){
            $flag=true;
        }
    }else{
        $flag=false;
    }
    return $flag;
}
//判断phone_number
function limit_phone_number($phone_number){
    $flag=false;
    $pattern = '/^1[3-9]\d{9}$/';
    if(preg_match($pattern, $phone_number)==1){
        $flag=true;
    }else{
        $flag=false;
    }
    return $flag;
}
//获取邮箱
function get_email($pdo,$session_id){
    try {
        // 准备查询语句
        $query = "SELECT email FROM session WHERE session_id = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $session_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['email'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取uid
function get_uid($pdo,$email){
    try {
        // 准备查询语句
        $query = "SELECT uid FROM users WHERE email = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $email);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['uid'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//判断是否是商家
function is_seller($pdo,$uid){
    try {
        // 准备查询语句
        $query = "SELECT is_seller FROM users WHERE uid = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['is_seller'];      //把查询结构赋值给$count
        if($result==1){
            return true;
        }else{
            return false;
        }
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//存储商家信息
function insert_seller($pdo,$arr){
    try {
        // 准备插入语句并绑定参数
        $query = "INSERT INTO sellers (user_uid, ID, name, age, phone_number) VALUES(?,?,?,?,?)";
        $statement = $pdo->prepare($query);
        // 执行插入操作
        $statement->execute($arr);
        
        // echo "Data inserted successfully.";
    } catch (PDOException $e) {
        echo "Error inserting data: " . $e->getMessage();
    }
}
//修改用户的is_seller信息
function update_seller($pdo,$uid){
    try {
        // 准备插入语句并绑定参数
        $query = "UPDATE users set is_seller=1 where uid=?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        
        // 执行插入操作
        $statement->execute();
        return 1;
        // echo "Data inserted successfully.";
    } catch (PDOException $e) {
        // echo "Error updata data: " . $e->getMessage();
        return 0;
    }
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {    //是post请求
    $json=file_get_contents('php://input');  //获取post请求中的原始数据
    $post_json=json_decode($json,true);       //把原始数据转化成json
    $response = array(     //初始化
        'status' => 0,
        'message' => 'registering',
    );
    $age=$post_json['age'];
    $ID=$post_json['ID'];
    $phone_number=$post_json['phone_number'];
    $session_id=$post_json['session_id'];
    $username=$post_json['username'];
    $uid=get_uid($pdo,get_email($pdo,$session_id));
    if(is_seller($pdo,$uid)){
        $response = array(
            'status' => 0,
            'message' => "已经是商家了",
        );
    }else{
        if(limit_age($age) && limit_ID($ID) && limit_phone_number($phone_number)){
        
            $arr=array($uid,$ID,$username,(int)$age,$phone_number);
            insert_seller($pdo,$arr);
            update_seller($pdo,$uid);
            $response = array(
                'status' => 1,
                'message' => 'ok',
            );
        }else{
            $response = array(
                'status' => 0,
                'message' => '信息有误，请输入真实信息',
            );
        }
    }
    

    echo json_encode($response);
}else{
    $response = array(     //初始化
        'status' => 0,
        'message' => '不是post',
    );
    echo json_encode($response);
}
?>